import numpy as np

def get_para(D, m):  # D:点集  m:精度
    n = len(D)
    m += 1  
    T = np.zeros((m,m))  # 系数矩阵
    for i in range(m):  # 获取系数矩阵
        for j in range(m):
            # print(D[i][0])
            # T[i][j] = (D[i][0]**(i+j)) * n
            s = 0
            for k in range(n):
                s += D[k][0]**(i+j)
            T[i][j] = s
    # a = np.zeros((n,1))
    y = np.zeros((m,1))  # 矩阵b
    for i in range(m):  # 获取矩阵b
        # y[i][0] = D[i][1]
        s = 0
        for k in range(n):
            s += (D[k][0]**i) * D[k][1]
        y[i][0] = s
    a = np.linalg.solve(T,y)  # 求解线性方程组,得到目标系数a
    return a


if __name__ == "__main__":
    # D = [(1,1), (2,4), (3,9),(4,16)]
    D = [(2,2), (4,11), (6,28), (8,40)]
    a = get_para(D, 1)
    print()
    print()
    for e in a[::-1]:
        print(e[0], end=" ")
    print()
    print()
    
    
    